<?php
namespace app\api\module;

use app\api\model\UserException;
use app\common\model\UserAddress;
use think\Db;

class Address extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [

        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】收货地址列表
            case ZFN_ADDRESS_LIST:
                return $this->list();
            //【1002】添加收货地址
            case ZFN_ADDRESS_ADD:
                return $this->addAddress();
            //【1003】获取某个收货地址信息
            case ZFN_ADDRESS_INFO:
                return $this->addressInfo();
            //【1004】编辑收货地址
            case ZFN_ADDRESS_EDIT:
                return $this->editAddress();
            //【1005】删除收货地址
            case ZFN_ADDRESS_REMOVE:
                return $this->removeAddress();
            //【1006】设为默认地址
            case ZFN_ADDRESS_DEFAULT:
                return $this->default();
            default:
                send_developer_error("模块【地址】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    //收货地址列表
    private function list()
    {
        $user = $this->currentUser();
        $res = UserAddress::list($user);
        $this->insertResponse('data', $res);
        return $this->response('收货地址列表');
    }

    //添加收货地址
    private function addAddress(){
        $user = $this->currentUser(true);
        $name = $this->essentialParam('name');
        $mobile = $this->essentialParam('mobile');
        $pro_id = $this->essentialParam('pro_id');
        $city_id = $this->essentialParam('city_id');
        $area_id = $this->essentialParam('area_id');
        $address = $this->essentialParam('address');
        $is_def = $this->essentialParam('is_def');
        $latitude = $this->essentialParam('latitude');
        $longitude = $this->essentialParam('longitude');
        $id = UserAddress::addAddress($user,$name,$mobile,$pro_id,$city_id,$area_id,$address,$is_def,$longitude,$latitude);
        $this->insertResponse('id',$id);
        return $this->response('新增收货地址成功');
    }

    private function addressInfo(){
        $user = $this->currentUser(true);
        $id = $this->essentialParam('id');
        $data = UserAddress::addressInfo($user,$id);
        $this->insertResponse('',$data);
        return $this->response('地址信息');
    }

    //编辑收货地址
    private function editAddress(){
        $user = $this->currentUser(true);
        $id = $this->essentialParam('id');
        $name = $this->essentialParam('name');
        $mobile = $this->essentialParam('mobile');
        $pro_id = $this->essentialParam('pro_id');
        $city_id = $this->essentialParam('city_id');
        $area_id = $this->essentialParam('area_id');
        $address = $this->essentialParam('address');
        $is_def = $this->essentialParam('is_def');
        $latitude = $this->essentialParam('latitude');
        $longitude = $this->essentialParam('longitude');
        UserAddress::editAddress($user,$id,$name,$mobile,$pro_id,$city_id,$area_id,$address,$is_def,$longitude,$latitude);
        return $this->response('编辑收货地址成功');
    }

    //删除收货地址
    private function removeAddress(){
        $user = $this->currentUser(true);
        $id = $this->essentialParam('id');
        UserAddress::removeAddress($user,$id);
        return $this->response('删除收货地址成功');
    }

    //设为默认地址
    private function default(){
        $user = $this->currentUser(true);
        $id = $this->essentialParam('id');
        UserAddress::default($user,$id);
        return $this->response('设为默认地址成功');
    }

}